/*
 * 作者:熊猫
 * 日期:2017 - 3 - 20 10 - 14 - 52
 *
 */

package net.lainiao.morethread.Chapter8.demo1;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/20.
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        MyLock lock=new MyLock();
        Thread threads[]=new Thread[5];
        for (int i = 0; i < threads.length; i++) {
            MyLock.Task task=lock.new Task(lock);
            threads[i]=new Thread(task);
            threads[i].start();
        }
        for (int i = 0; i < 15; i++) {
            System.out.println("Main: Logging the lock");
            System.out.println("****************************************");
            System.out.println("Lock: Owner "+lock.getOwnerName());
            System.out.println("Lock: Queued Threads "+lock.hasQueuedThreads());
            if(lock.hasQueuedThreads()){
                System.out.println("Lock Queed Length "+lock.getQueueLength());
                System.out.println("Lock:Queued Length:");
                Collection<Thread> lockedThreads=lock.getThreads();
                for(Thread lockedThread:lockedThreads){
                    System.out.println(lockedThread.getName());
                }
                System.out.println("");
            }
            System.out.println("Lock: Fairness:"+lock.isFair());
            System.out.println("Lock: Locked :"+lock.isLocked());
            System.out.println("*******************************");
            TimeUnit.SECONDS.sleep(1);
            System.out.println("Main Over");
        }
    }
}
